package com.company.controller;

import com.alibaba.fastjson.JSONObject;
import com.company.base.User;
import com.company.core.annotation.Login;
import com.company.core.result.Result;
import com.company.core.result.ResultUtil;
import com.company.core.validator.ValidatorUtils;
import com.company.core.validator.group.AddGroup;
import com.company.pushTools.PushParams;
import com.company.pushTools.PushUtils;
import com.company.utils.Constant;
import com.company.utils.GzhUtils;
import com.company.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: smart-company
 * @description: 推送相关API
 * @author: jyz
 * @create: 2019-04-01 09:42
 **/
@Api(value = "推送")
@RestController
@RequestMapping("api/push")
public class PushController {
    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
    @Autowired
    GzhUtils gzhUtils;
    @Autowired
    PushUtils pushUtils;
    @Autowired
    private RedisUtils R;

    /**
     * 推送消息
     * @return
     */
    @Login
    @PostMapping("/sendMeg")
    @ApiOperation(value = "通用模板消息推送")
    public Result sendMeg(HttpServletRequest request, @ApiParam("pushParamsQuery") @RequestBody PushParams pushParamsQuery){
        ValidatorUtils.validateEntity(pushParamsQuery,AddGroup.class);
        String phone = (String) request.getSession().getAttribute("phone");
        User user = R.get(Constant.CompanyUser+phone,User.class);
        if (user == null){
            return ResultUtil.error(1,"登录失效，请重新登录");
        }

        Result result = ResultUtil.success();
        String accessToken = gzhUtils.getAccessToken(user.getAppId());

        if (pushParamsQuery.getRange() == 2){
            JSONObject openIdsjsonObject = gzhUtils.getFollowList(accessToken,null);
            JSONObject jsonObject = (JSONObject) openIdsjsonObject.get("data");
            List<String> openIds = (List<String>) jsonObject.get("openid");
            pushParamsQuery.setOpenIdList(openIds);
        }else {

        }
        pushParamsQuery.setAccessToken(accessToken);
        pushUtils.pushByParams(pushParamsQuery);

        return result;
    }



//    if (pushParamsQuery.getRange() == 1){
//        AssertUtils.isNull(pushParamsQuery.getTouser(),"自己的openId不能为空");
//        JSONObject openIdsjsonObject = gzhUtils.getFollowList(accessToken,null);
//        JSONObject jsonObject = (JSONObject) openIdsjsonObject.get("data");
//        List<String> openIds = (List<String>) jsonObject.get("openid");
//        pushParamsQuery.setOpenIdList(openIds);
//    }else {
//        JSONObject openIdsjsonObject = gzhUtils.getFollowList(accessToken,null);
//        JSONObject jsonObject = (JSONObject) openIdsjsonObject.get("data");
//        List<String> openIds = (List<String>) jsonObject.get("openid");
//        pushParamsQuery.setOpenIdList(openIds);
//    }
//        pushParamsQuery.setAccessToken(accessToken);
//        pushUtils.pushByParams(pushParamsQuery);
//        return result;

}
